package com.dao;

import com.entity.Td_tblpartyorgmembers;
import com.model.MemberBranchModel;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

@Transactional
public interface MemberDao extends JpaRepository<Td_tblpartyorgmembers, Integer>, JpaSpecificationExecutor<Td_tblpartyorgmembers>, Serializable {

    /**
     * 查询所有用户
     *
     * @return
     */
    @Query(value = "select * from Td_tblpartyorgmembers where fd_menbertype=5", nativeQuery = true)
    public List<Td_tblpartyorgmembers> getAllMember();

    /**
     * 根据党组织查询该组织下的所有用户
     *
     * @param branchId
     * @return
     */
    @Query(value = "select * from Td_tblpartyorgmembers where fd_menbertype=5 and fd_branchId=?", nativeQuery = true)
    public List<Td_tblpartyorgmembers> getAllByFd_branchId(int branchId);

    /**
     * 根据党组织查询所有申请
     * @param type 流程(1=申请人员，2=积极分子，3=发展对象，4=预备党员，5=正式党员)
     * @param branchId 党组织id
     * @return
     */
    @Query(value = "select * from Td_tblpartyorgmembers  where Fd_menbertype=? and Fd_isType=2 and fd_branchId=?",nativeQuery = true)
    public List<Td_tblpartyorgmembers> getAllApplyByBranchId(int type,int branchId);

    /**
     * 查询所有申请
     * @param type 流程(1=申请人员，2=积极分子，3=发展对象，4=预备党员，5=正式党员)
     * @return
     */
    @Query(value = "select * from Td_tblpartyorgmembers  where Fd_menbertype=? and Fd_isType=2 ",nativeQuery = true)
    public List<Td_tblpartyorgmembers> getAllApply(int type);

    /**
     * 根据党组织查询所有待审批
     * @param type 流程(1=申请人员，2=积极分子，3=发展对象，4=预备党员，5=正式党员)
     * @param branchId 党组织id
     * @return
     */
    @Query(value = "select * from Td_tblpartyorgmembers  where Fd_menbertype=? and Fd_isType=1 and fd_branchId=?",nativeQuery = true)
    public List<Td_tblpartyorgmembers> getApprovalByBranchId(int type,int branchId,Pageable pageable);

    /**
     * 查询所有待审批
     * @param type 流程(1=申请人员，2=积极分子，3=发展对象，4=预备党员，5=正式党员)
     * @return
     */
    @Query(value = "select fd_loginId,fd_branchName,fd_userName,fd_imageurl from Td_tblpartyorgmembers  where Fd_menbertype=? and fd_userName like ? and Fd_isType=2 ",nativeQuery = true)
    public List<Object[]> getAllApprroval(int type,String name,Pageable pageable);

    /**
     * 根据姓名查询所有正式党员(分页)
     * @param Name 姓名
     * @return 0=用户id   1=组织名   2=用户名   3=头像地址
     */
    @Query(value = "select fd_loginId,fd_branchName,fd_userName,Fd_ImageUrl,fd_activiststrain from Td_tblpartyorgmembers where fd_userName like ?",nativeQuery = true)
    public List<Object[]> getMemberByName(String Name, Pageable pageable);

    /**
     * 根据姓名查询所有正式党员(不分页)
     * @param Name 姓名
     * @return 0=用户id   1=组织名   2=用户名   3=头像地址
     */
    @Query(value = "select fd_loginId,fd_branchName,fd_userName,Fd_ImageUrl from Td_tblpartyorgmembers where fd_userName like ?",nativeQuery = true)
    public List<Object[]> getMemberByName(String Name);

    @Query(value = "select * from Td_tblpartyorgmembers where fd_menbertype=5",nativeQuery = true)
    public List<Td_tblpartyorgmembers> getAllPartyMember();

    /**
     * 根据用户名查询所有信息
     * @param loginId
     * @return
     */
    @Query(value = "select * from Td_tblpartyorgmembers where fd_loginId=?",nativeQuery = true)
    public Td_tblpartyorgmembers getByFd_loginId(int loginId);

    /**
     * 修改用户所属组织
     * @param branchId 组织id
     * @param branchName 组织名
     * @param loginId 用户id
     */
    @Query(value = "update Td_tblpartyorgmembers set fd_branchId=? , fd_branchName=? where fd_loginId=?",nativeQuery = true)
    @Modifying
    public int updateBranchByLoginId(int branchId,String branchName,int loginId);

    /**
     * 修改用户组织迁移模块
     * @param state 状态(0=未迁移，1=正在迁移)
     * @param loginId 用户id
     * @return
     */
    @Query(value = "update Td_tblpartyorgmembers set fd_activistsdate=? where fd_loginId=?",nativeQuery = true)
    @Modifying
    public int updateTransferState(int state,int loginId);
}
